package com.zyzd.baseretrofit.base.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zyzd.baseretrofit.interf.UiBaseInterface;
import com.zyzd.commontools.other.AntiShake;

import java.io.Serializable;

import butterknife.ButterKnife;


/**
 * Created by 李宗源 on 2016/8/3.
 */
public abstract class BaseFragment extends Fragment implements UiBaseInterface {
    public View mRoot;
    protected Bundle mBundle;
    protected AntiShake antiShake = new AntiShake();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = getArguments();
        initBundle(mBundle);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRoot != null) {
            ViewGroup parent = (ViewGroup) mRoot.getParent();
            if (parent != null)
                parent.removeView(mRoot);
        } else {//初次创建
            mRoot = inflater.inflate(getLayoutId(), container, false);
            ButterKnife.bind(this, mRoot);
            initWidght(mRoot, inflater);
            initData();//此处非首次请求数据
            setListener();
        }
        return mRoot;
    }

    public void initWidght(View mRoot, LayoutInflater inflater) {//用于初始化视图
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRoot = null;
        mBundle = null;
        ButterKnife.unbind(this);
    }

    protected void initBundle(Bundle bundle) {
    }

    //----------------------------------工具方法------------------------------------

    /**
     * 获取传递过来的序列化数据
     */
    protected <T extends Serializable> T getBundleSerializable(String key) {
        if (mBundle == null) {
            return null;
        }
        return (T) mBundle.getSerializable(key);
    }

    /**
     * 找控件的方法
     */
    protected <T extends View> T findView(int viewId) {
        return (T) mRoot.findViewById(viewId);
    }

    protected void setText(int viewId, String text) {
        TextView textView = findView(viewId);
        if (TextUtils.isEmpty(text)) {
            return;
        }
        textView.setText(text);
    }

    protected void setText(int viewId, String text, String emptyTip) {
        TextView textView = findView(viewId);
        if (TextUtils.isEmpty(text)) {
            textView.setText(emptyTip);
            return;
        }
        textView.setText(text);
    }

    protected void setTextEmptyGone(int viewId, String text) {
        TextView textView = findView(viewId);
        if (TextUtils.isEmpty(text)) {
            textView.setVisibility(View.GONE);
            return;
        }
        textView.setText(text);
    }

    protected void setGone(int id) {
        findView(id).setVisibility(View.GONE);
    }

    protected void setVisibility(int id) {
        findView(id).setVisibility(View.VISIBLE);
    }

    protected void setInVisibility(int id) {
        findView(id).setVisibility(View.INVISIBLE);
    }

    public String getSimpleName() {
        return getClass().getSimpleName();
    }

    public void showToast(String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        Toast.makeText(getContext().getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
